package ar.edu.itba.it.cg.grupo01.impl;

import java.util.Iterator;

import ar.edu.itba.it.cg.grupo01.api.CameraInterface;
import ar.edu.itba.it.cg.grupo01.api.Ray;
import static com.google.common.base.Preconditions.*;

/**
 * Iterator that
 * 
 */
public class SingleRay implements Iterator<CameraRay> {

	private final CameraInterface camera;
	private final int width;
	private final int height;
	private int i;
	private int j;

	public SingleRay(final CameraInterface camera) {
		checkNotNull(camera, "Camera should not be null");
		this.camera = camera;
		this.width = camera.getxResolution();
		this.height = camera.getyResolution();
		initialize();
	}

	private void initialize() {
		this.i = 0;
		this.j = 0;
	}

	@Override
	public boolean hasNext() {
		return (i < height) && (j < width);
	}

	@Override
	public CameraRay next() {
		Ray ray = camera.getRay(i, j);
		CameraRay cameraRay = new CameraRay(ray, i, j);
		this.j = (j + 1) % width;

		// As we already analyze the (0,0) then no harm is done
		this.i = (j == 0) ? i + 1 : i;
		
		return cameraRay;
	}

	@Override
	public void remove() {
		throw new UnsupportedOperationException();
	}

}
